using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace ICP_MS.Plugin.Experiment;

public class FormTransferMass : Form
{
	private ExperimentDatas experiment = null;

	private ExperimentDB db = null;

	private HashSet<int> MassSelected = new HashSet<int>();

	private List<AnalyteIsotope> tempIsotopes = new List<AnalyteIsotope>();

	private IContainer components = null;

	private Panel panel1;

	private FlowLayoutPanel flowLayoutPanel2;

	private DataGridView dgvSelectIsotopes;

	private FlowLayoutPanel flowLayoutPanel1;

	private CustomControl SelectMassControl;

	private DataGridViewTextBoxColumn dataGridViewTextBoxColumn1;

	private DataGridViewTextBoxColumn dataGridViewTextBoxColumn2;

	private Button button1;

	private DataGridViewTextBoxColumn Column1;

	private DataGridViewTextBoxColumn Column2;

	public FormTransferMass(ExperimentDatas experiment, ExperimentDB db)
	{
		InitializeComponent();
		this.experiment = experiment;
		this.db = db;
	}

	private void FormTransferMass_Load(object sender, EventArgs e)
	{
		Win32API.SetTopMost((Control)this);
		tempIsotopes = experiment.SelectIsotopes;
		RebindSelectIsotopes();
		experiment.SelectIsotopes.ForEach(delegate(AnalyteIsotope selectIsotope)
		{
			MassSelected.Add(selectIsotope.intMass);
		});
		SelectMassControl.SelectMassList = MassSelected;
		SelectMassControl.ValueChanged += SelectMassControl_ValueChanged;
	}

	private void RebindSelectIsotopes()
	{
		dgvSelectIsotopes.AutoGenerateColumns = false;
		experiment.SelectIsotopes.Sort((AnalyteIsotope s1, AnalyteIsotope s2) => s1.mass.CompareTo(s2.mass));
		if (dgvSelectIsotopes.DataSource == null)
		{
			dgvSelectIsotopes.DataSource = experiment.SelectIsotopes;
		}
		else
		{
			BindingContext[dgvSelectIsotopes.DataSource].SuspendBinding();
			dgvSelectIsotopes.DataSource = null;
			dgvSelectIsotopes.DataSource = experiment.SelectIsotopes;
			BindingContext[dgvSelectIsotopes.DataSource].ResumeBinding();
		}
		for (int i = 0; i < dgvSelectIsotopes.Rows.Count; i++)
		{
			dgvSelectIsotopes.Rows[i].HeaderCell.Value = (i + 1).ToString();
		}
	}

	private void RebindSelectTempIsotopes()
	{
		dgvSelectIsotopes.AutoGenerateColumns = false;
		experiment.SelectIsotopes.Sort((AnalyteIsotope s1, AnalyteIsotope s2) => s1.mass.CompareTo(s2.mass));
		if (dgvSelectIsotopes.DataSource == null)
		{
			dgvSelectIsotopes.DataSource = tempIsotopes;
		}
		else
		{
			BindingContext[dgvSelectIsotopes.DataSource].SuspendBinding();
			dgvSelectIsotopes.DataSource = null;
			dgvSelectIsotopes.DataSource = tempIsotopes;
			BindingContext[dgvSelectIsotopes.DataSource].ResumeBinding();
		}
		for (int i = 0; i < dgvSelectIsotopes.Rows.Count; i++)
		{
			dgvSelectIsotopes.Rows[i].HeaderCell.Value = (i + 1).ToString();
		}
	}

	private void SelectMassControl_ValueChanged(object sender, EventArgs e)
	{
		int mass = decimal.ToInt32((sender as CustomControl).Value);
		if (!SelectMassControl.SelectMassList.Contains(mass))
		{
			AnalyteIsotope SelectAnalyteIsotope = db.GetAnalyteIsotopeByIntMass(mass);
			if (SelectAnalyteIsotope != null && !tempIsotopes.Contains(SelectAnalyteIsotope))
			{
				tempIsotopes.Add(SelectAnalyteIsotope);
				RebindSelectTempIsotopes();
				dgvSelectIsotopes.Refresh();
				return;
			}
			AnalyteIsotope isotopeinfo = new AnalyteIsotope();
			isotopeinfo.mass = mass;
			isotopeinfo.intMass = mass;
			isotopeinfo.symbol = mass.ToString();
			isotopeinfo.atomicSymbol = mass.ToString();
			tempIsotopes.Add(isotopeinfo);
			RebindSelectTempIsotopes();
			dgvSelectIsotopes.Refresh();
		}
		else
		{
			AnalyteIsotope isotopeinfo2 = tempIsotopes.Find((AnalyteIsotope r) => r.id == mass);
			if (isotopeinfo2 != null)
			{
				tempIsotopes.Remove(isotopeinfo2);
				RebindSelectTempIsotopes();
				dgvSelectIsotopes.Refresh();
			}
		}
	}

	private void dgvSelectIsotopes_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
	{
		if (dgvSelectIsotopes.SelectedRows != null && dgvSelectIsotopes.SelectedRows.Count > 0 && e.Button == MouseButtons.Right)
		{
			ContextMenu menu = new ContextMenu();
			menu.MenuItems.Add(Lang.Txt("FormIsotopes.Menu.Delete.Text"), delegate
			{
				DeleteSelectedIsotopes();
			});
			menu.Show(dgvSelectIsotopes, e.Location);
		}
	}

	private void dgvSelectIsotopes_KeyDown(object sender, KeyEventArgs e)
	{
		if (dgvSelectIsotopes.SelectedRows != null && dgvSelectIsotopes.SelectedRows.Count > 0 && e.KeyCode == Keys.Delete)
		{
			DeleteSelectedIsotopes();
		}
	}

	private void DeleteSelectedIsotopes()
	{
		List<AnalyteIsotope> isotopes = new List<AnalyteIsotope>();
		foreach (DataGridViewRow row in dgvSelectIsotopes.SelectedRows)
		{
			isotopes.Add(tempIsotopes[row.Index]);
		}
		foreach (AnalyteIsotope isotope in isotopes)
		{
			if (db.DeleteSelectedIsotope(isotope))
			{
				experiment.SelectIsotopes.Remove(isotope);
				tempIsotopes.Remove(isotope);
			}
		}
		RebindSelectIsotopes();
	}

	private void button1_Click(object sender, EventArgs e)
	{
		base.DialogResult = DialogResult.OK;
		experiment.SelectIsotopes = tempIsotopes;
	}

	protected override void Dispose(bool disposing)
	{
		if (disposing && components != null)
		{
			components.Dispose();
		}
		base.Dispose(disposing);
	}

	private void InitializeComponent()
	{
		System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(ICP_MS.Plugin.Experiment.FormTransferMass));
		this.panel1 = new System.Windows.Forms.Panel();
		this.button1 = new System.Windows.Forms.Button();
		this.flowLayoutPanel2 = new System.Windows.Forms.FlowLayoutPanel();
		this.dgvSelectIsotopes = new System.Windows.Forms.DataGridView();
		this.flowLayoutPanel1 = new System.Windows.Forms.FlowLayoutPanel();
		this.dataGridViewTextBoxColumn1 = new System.Windows.Forms.DataGridViewTextBoxColumn();
		this.dataGridViewTextBoxColumn2 = new System.Windows.Forms.DataGridViewTextBoxColumn();
		this.Column1 = new System.Windows.Forms.DataGridViewTextBoxColumn();
		this.Column2 = new System.Windows.Forms.DataGridViewTextBoxColumn();
		this.SelectMassControl = new ICP_MS.Plugin.Experiment.CustomControl();
		this.panel1.SuspendLayout();
		this.flowLayoutPanel2.SuspendLayout();
		((System.ComponentModel.ISupportInitialize)this.dgvSelectIsotopes).BeginInit();
		this.flowLayoutPanel1.SuspendLayout();
		base.SuspendLayout();
		this.panel1.Controls.Add(this.button1);
		this.panel1.Controls.Add(this.flowLayoutPanel2);
		this.panel1.Controls.Add(this.flowLayoutPanel1);
		this.panel1.Dock = System.Windows.Forms.DockStyle.Fill;
		this.panel1.Location = new System.Drawing.Point(0, 0);
		this.panel1.Name = "panel1";
		this.panel1.Size = new System.Drawing.Size(991, 537);
		this.panel1.TabIndex = 0;
		this.button1.Location = new System.Drawing.Point(779, 487);
		this.button1.Name = "button1";
		this.button1.Size = new System.Drawing.Size(87, 30);
		this.button1.TabIndex = 3;
		this.button1.Text = "确定";
		this.button1.UseVisualStyleBackColor = true;
		this.button1.Click += new System.EventHandler(button1_Click);
		this.flowLayoutPanel2.AutoScroll = true;
		this.flowLayoutPanel2.Controls.Add(this.dgvSelectIsotopes);
		this.flowLayoutPanel2.Location = new System.Drawing.Point(673, 15);
		this.flowLayoutPanel2.Name = "flowLayoutPanel2";
		this.flowLayoutPanel2.Size = new System.Drawing.Size(306, 466);
		this.flowLayoutPanel2.TabIndex = 2;
		this.dgvSelectIsotopes.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
		this.dgvSelectIsotopes.Columns.AddRange(this.Column1, this.Column2);
		this.dgvSelectIsotopes.Location = new System.Drawing.Point(3, 3);
		this.dgvSelectIsotopes.Name = "dgvSelectIsotopes";
		this.dgvSelectIsotopes.RowHeadersWidth = 51;
		this.dgvSelectIsotopes.RowTemplate.Height = 27;
		this.dgvSelectIsotopes.Size = new System.Drawing.Size(279, 465);
		this.dgvSelectIsotopes.TabIndex = 0;
		this.dgvSelectIsotopes.CellMouseDown += new System.Windows.Forms.DataGridViewCellMouseEventHandler(dgvSelectIsotopes_CellMouseDown);
		this.dgvSelectIsotopes.KeyDown += new System.Windows.Forms.KeyEventHandler(dgvSelectIsotopes_KeyDown);
		this.flowLayoutPanel1.Controls.Add(this.SelectMassControl);
		this.flowLayoutPanel1.Location = new System.Drawing.Point(12, 12);
		this.flowLayoutPanel1.Name = "flowLayoutPanel1";
		this.flowLayoutPanel1.Size = new System.Drawing.Size(655, 471);
		this.flowLayoutPanel1.TabIndex = 1;
		this.dataGridViewTextBoxColumn1.DataPropertyName = "mass";
		this.dataGridViewTextBoxColumn1.FillWeight = 20f;
		this.dataGridViewTextBoxColumn1.HeaderText = "质量数";
		this.dataGridViewTextBoxColumn1.MinimumWidth = 6;
		this.dataGridViewTextBoxColumn1.Name = "dataGridViewTextBoxColumn1";
		this.dataGridViewTextBoxColumn1.Width = 125;
		this.dataGridViewTextBoxColumn2.DataPropertyName = "symbol";
		this.dataGridViewTextBoxColumn2.FillWeight = 20f;
		this.dataGridViewTextBoxColumn2.HeaderText = "元素";
		this.dataGridViewTextBoxColumn2.MinimumWidth = 6;
		this.dataGridViewTextBoxColumn2.Name = "dataGridViewTextBoxColumn2";
		this.dataGridViewTextBoxColumn2.Width = 125;
		this.Column1.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.Fill;
		this.Column1.DataPropertyName = "intMass";
		this.Column1.FillWeight = 20f;
		this.Column1.HeaderText = "质量数";
		this.Column1.MinimumWidth = 6;
		this.Column1.Name = "Column1";
		this.Column2.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.Fill;
		this.Column2.DataPropertyName = "symbol";
		this.Column2.FillWeight = 20f;
		this.Column2.HeaderText = "元素";
		this.Column2.MinimumWidth = 6;
		this.Column2.Name = "Column2";
		this.SelectMassControl.Location = new System.Drawing.Point(3, 3);
		this.SelectMassControl.Name = "SelectMassControl";
		this.SelectMassControl.SelectMassList = (System.Collections.Generic.HashSet<int>)resources.GetObject("SelectMassControl.SelectMassList");
		this.SelectMassControl.Size = new System.Drawing.Size(652, 466);
		this.SelectMassControl.TabIndex = 0;
		this.SelectMassControl.Value = 0;
		base.AutoScaleDimensions = new System.Drawing.SizeF(8f, 15f);
		base.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
		base.ClientSize = new System.Drawing.Size(991, 537);
		base.Controls.Add(this.panel1);
		base.Name = "FormTransferMass";
		this.Text = "FormTransferMass";
		base.Load += new System.EventHandler(FormTransferMass_Load);
		this.panel1.ResumeLayout(false);
		this.flowLayoutPanel2.ResumeLayout(false);
		((System.ComponentModel.ISupportInitialize)this.dgvSelectIsotopes).EndInit();
		this.flowLayoutPanel1.ResumeLayout(false);
		base.ResumeLayout(false);
	}
}
